using System;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;

namespace Os.WebUI.Helpers
{
	public static class AcessoHelper
	{

        /// <summary>
        /// Carrega o menu conforme o papel do usuário autenticado
        /// </summary>
        /// <param name="helper"></param>
        /// <returns></returns>
		public static string controleAcesso(this HtmlHelper helper)
		{
            string html = string.Empty;
            string url = HttpContext.Current.Request.Path.ToString();

            if (HttpContext.Current.Request.IsAuthenticated)
            {
                MembershipUser user = Membership.GetUser();
                if (Roles.IsUserInRole(user.UserName, "Administrador"))
                {

                    //Ordens de Serviço Pendentes
                    if (url == AcessoHelper.getApplicationStart() + "/OrdemServico/OrdensServicoPendentes")
                    {
                        html += "\r\n <li><a class='ativo' href='" + AcessoHelper.getApplicationStart() + "/OrdemServico/OrdensServicoPendentes'>O.S. Pendentes</a></li>";
                    }
                    else
                    {
                        html += "\r\n <li><a href='" + AcessoHelper.getApplicationStart() + "/OrdemServico/OrdensServicoPendentes'>O.S. Pendentes</a></li>";
                    }

                    //Ordens de Serviço
                    if (
                            url.ToLower().Contains(AcessoHelper.getApplicationStart() + "/Os".ToLower()) ||
                            url.Contains(AcessoHelper.getApplicationStart() + "/OrdemServico/Editar/") ||
                            url.Contains(AcessoHelper.getApplicationStart() + "/OrdemServico/Detalhes/") ||
                            url.Contains(AcessoHelper.getApplicationStart() + "/OrdemServico/AberturaOrdemservicoSucesso") ||
                            url.Contains(AcessoHelper.getApplicationStart() + "/OrdemServico/OrdemServicoSucesso")
                       )
                    {
                        html += "\r\n <li><a class='ativo' href='" + AcessoHelper.getApplicationStart() + "/os'>Ordens de Serviço</a></li>";
                    }
                    else
                    {
                        html += "\r\n <li><a href='" + AcessoHelper.getApplicationStart() + "/Os'>Ordens de Serviço</a></li>";
                    }

                    //Equipamentos
                    if 
                        (
                            url == AcessoHelper.getApplicationStart() + "/Equipamento" ||
                            url.Contains(AcessoHelper.getApplicationStart() + "/Equipamento/Editar/") ||
                            url.Contains(AcessoHelper.getApplicationStart() + "/Equipamento/Detalhes/") ||
                            url == AcessoHelper.getApplicationStart() + "/Equipamento/Novo" 
                        )
                    {
                        html += "\r\n <li><a class='ativo' href='" + AcessoHelper.getApplicationStart() + "/Equipamento'>Equipamentos</a></li>";
                    }
                    else
                    {
                        html += "\r\n <li><a href='" + AcessoHelper.getApplicationStart() + "/Equipamento'>Equipamentos</a></li>";
                    }

                    //Clientes
                    if 
                        (
                            url == AcessoHelper.getApplicationStart() + "/Cliente" ||
                            url.Contains(AcessoHelper.getApplicationStart() + "/Cliente/Detalhes/") ||
                            url.Contains(AcessoHelper.getApplicationStart() + "/Cliente/Editar/") ||
                            url.Contains(AcessoHelper.getApplicationStart() + "/Cliente/ClienteSalvoComSucesso") ||
                            url == AcessoHelper.getApplicationStart() + "/Cliente/Novo"
                        )
                    {
                        html += "\r\n <li><a class='ativo' href='" + AcessoHelper.getApplicationStart() + "/Cliente'>Clientes</a></li>";
                    }
                    else
                    {
                        html += "\r\n <li><a href='" + AcessoHelper.getApplicationStart() + "/Cliente'>Clientes</a></li>";
                    }
                    
                    //Conta Administrativa
                    if 
                        (
                            url == AcessoHelper.getApplicationStart() + "/Account/Register" ||
                            url == AcessoHelper.getApplicationStart() + "/Account/ChangePasswordSuccess" || 
                            url == AcessoHelper.getApplicationStart() + "/Account/ChangePassword" 
                        )
                    {
                        html += "\r\n <li><a class='ativo' href='" + AcessoHelper.getApplicationStart() + "/Account/Register'>Conta Administrativa</a></li>";
                    }
                    else
                    {
                        html += "\r\n <li><a href='" + AcessoHelper.getApplicationStart() + "/Account/Register'>Conta Administrativa</a></li>";
                    }

                    //Relatórios
                    if 
                        (
                            url == AcessoHelper.getApplicationStart() + "/Relatorios" ||
                            url == AcessoHelper.getApplicationStart() + "/Relatorios/Equipamentos" ||
                            url == AcessoHelper.getApplicationStart() + "/Relatorios/OrdensServico" ||
                            url == AcessoHelper.getApplicationStart() + "/Relatorios/Clientes" ||
                            url == AcessoHelper.getApplicationStart() + "/Relatorios/VigenciaContrato" 
                        )
                    {
                        html += "\r\n <li><a class='ativo' href='" + AcessoHelper.getApplicationStart() + "/Relatorios'>Relatórios</a></li>";
                    }
                    else
                    {
                        html += "\r\n <li><a href='" + AcessoHelper.getApplicationStart() + "/Relatorios'>Relatórios</a></li>";
                    }
                }
                else
                {
                    if (Roles.IsUserInRole(user.UserName, "Cliente"))
                    {
                        if (
                                url == AcessoHelper.getApplicationStart() + "/OrdemServico/MinhasOrdensServico" ||
                                url.Contains(AcessoHelper.getApplicationStart() + "/OrdemServico/DetalhesOrdemServico")
                            )
                        {
                            html += "\r\n <li><a class='ativo' href='" + AcessoHelper.getApplicationStart() + "/OrdemServico/MinhasOrdensServico'>Minhas O.S.</a></li>";
                        }
                        else
                        {
                            html += "\r\n <li><a href='" + AcessoHelper.getApplicationStart() + "/OrdemServico/MinhasOrdensServico'>Minhas O.S.</a></li>";
                        }

                        if (
                                url == AcessoHelper.getApplicationStart() + "/OrdemServico/AberturaOrdemServico" ||
                                url == AcessoHelper.getApplicationStart() + "/OrdemServico/AberturaOrdemServicoSucesso"
                           )
                        {
                            html += "\r\n <li><a class='ativo' href='" + AcessoHelper.getApplicationStart() + "/OrdemServico/AberturaOrdemServico'>Abertura de O.S.</a></li>";
                        }
                        else
                        {
                            html += "\r\n <li><a href='" + AcessoHelper.getApplicationStart() + "/OrdemServico/AberturaOrdemServico'>Abertura de O.S.</a></li>";
                        }

                        if 
                            (
                                url == AcessoHelper.getApplicationStart() + "/Account" ||
                                url == AcessoHelper.getApplicationStart() + "/Account/ChangePassword" ||
                                url == AcessoHelper.getApplicationStart() + "/Account/ChangePasswordSuccess"
                            )
                        {
                            html += "\r\n <li><a class='ativo' href='" + AcessoHelper.getApplicationStart() + "/Account/ChangePassword'>Alterar Senha</a></li>";
                        }
                        else
                        {
                            html += "\r\n <li><a href='" + AcessoHelper.getApplicationStart() + "/Account/ChangePassword'>Alterar Senha</a></li>";
                        }
                    }
                }
            }
            return html;
		}
            
        /// <summary>
        /// Obtem o diretório inicial da aplicação
        /// </summary>
        /// <returns></returns>
		private static string getApplicationStart()
		{
			string text = HttpContext.Current.Request.ApplicationPath.ToString();
			
			if (text == "/")
				return "";
			else
				return text;
		}

        /// <summary>
        /// Obtem o diretório base da aplicação
        /// </summary>
        /// <param name="helper"></param>
        /// <returns></returns>
        public static string getDiretorioBase(this HtmlHelper helper)
        {
           string path = HttpContext.Current.Request.ApplicationPath.ToString();

           if (path.Contains("suporte"))
           {
               return "/suporte/";
           }
           else
           {
               return "/";
           }
        }

        /// <summary>
        /// Realiza as referências de arquivos css
        /// </summary>
        /// <param name="helper"></param>
        /// <returns></returns>
        public static string LoadCss(this HtmlHelper helper)
        {
            string html = string.Empty;
            html += "\r\n" + @" <link href='" + getDiretorioBase(helper) + "Content/Site.css' rel='stylesheet' type='text/css' />";
            html += "\r\n" + @" <link href='" + getDiretorioBase(helper) + "Content/jquery-ui-1.8.5.custom.css' rel='stylesheet' type='text/css' />";
            html += "\r\n" + @" <link href='" + getDiretorioBase(helper) + "Scripts/themes/ui-lightness/jquery.ui.datepicker.css' rel='stylesheet' type='text/css' />";
            return html;
        }

        /// <summary>
        /// realiza a verificação se um determinado usuário pertence a um papel
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="userName"></param>
        /// <param name="roleName"></param>
        /// <returns></returns>
        public static bool BelongToUser(this HtmlHelper helper, string userName, string roleName)
        {
            return userName != null && Roles.IsUserInRole(userName, roleName);
        }
	}
}
